#ifndef THINK_THINK_DB_BUILDER_MYSQL_H
#define THINK_THINK_DB_BUILDER_MYSQL_H

static char* think_db_builder_mysql =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\db\\builder;\n"
	"\n"
	"use think\\db\\Builder;\n"
	"use think\\Exception;\n"
	"\n"
	"/**\n"
	" * mysql数据库驱动\n"
	" */\n"
	"class Mysql extends Builder\n"
	"{\n"
	"\n"
	"    protected $insertAllSql = \'%INSERT% INTO %TABLE% (%FIELD%) VALUES %DATA% %COMMENT%\';\n"
	"    protected $updateSql    = \'UPDATE %TABLE% %JOIN% SET %SET% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%\';\n"
	"\n"
	"    /**\n"
	"     * 生成insertall SQL\n"
	"     * @access public\n"
	"     * @param array     $dataSet 数据集\n"
	"     * @param array     $options 表达式\n"
	"     * @param bool      $replace 是否replace\n"
	"     * @return string\n"
	"     * @throws Exception\n"
	"     */\n"
	"    public function insertAll($dataSet, $options = [], $replace = false)\n"
	"    {\n"
	"        // 获取合法的字段\n"
	"        if (\'*\' == $options[\'field\']) {\n"
	"            $fields = array_keys($this->query->getFieldsType($options[\'table\']));\n"
	"        } else {\n"
	"            $fields = $options[\'field\'];\n"
	"        }\n"
	"\n"
	"        foreach ($dataSet as $data) {\n"
	"            foreach ($data as $key => $val) {\n"
	"                if (!in_array($key, $fields, true)) {\n"
	"                    if ($options[\'strict\']) {\n"
	"                        throw new Exception(\'fields not exists:[\' . $key . \']\');\n"
	"                    }\n"
	"                    unset($data[$key]);\n"
	"                } elseif (is_null($val)) {\n"
	"                    $data[$key] = \'NULL\';\n"
	"                } elseif (is_scalar($val)) {\n"
	"                    $data[$key] = $this->parseValue($val, $key);\n"
	"                } elseif (is_object($val) && method_exists($val, \'__toString\')) {\n"
	"                    // 对象数据写入\n"
	"                    $data[$key] = $val->__toString();\n"
	"                } else {\n"
	"                    // 过滤掉非标量数据\n"
	"                    unset($data[$key]);\n"
	"                }\n"
	"            }\n"
	"            $value    = array_values($data);\n"
	"            $values[] = \'( \' . implode(\',\', $value) . \' )\';\n"
	"\n"
	"            if (!isset($insertFields)) {\n"
	"                $insertFields = array_map([$this, \'parseKey\'], array_keys($data));\n"
	"            }\n"
	"        }\n"
	"\n"
	"        return str_replace(\n"
	"            [\'%INSERT%\', \'%TABLE%\', \'%FIELD%\', \'%DATA%\', \'%COMMENT%\'],\n"
	"            [\n"
	"                $replace ? \'REPLACE\' : \'INSERT\',\n"
	"                $this->parseTable($options[\'table\'], $options),\n"
	"                implode(\' , \', $insertFields),\n"
	"                implode(\' , \', $values),\n"
	"                $this->parseComment($options[\'comment\']),\n"
	"            ], $this->insertAllSql);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 字段和表名处理\n"
	"     * @access protected\n"
	"     * @param mixed  $key\n"
	"     * @param array  $options\n"
	"     * @return string\n"
	"     */\n"
	"    protected function parseKey($key, $options = [], $strict = false)\n"
	"    {\n"
	"        if (is_numeric($key)) {\n"
	"            return $key;\n"
	"        } elseif ($key instanceof Expression) {\n"
	"            return $key->getValue();\n"
	"        }\n"
	"\n"
	"        $key = trim($key);\n"
	"        if (strpos($key, \'$.\') && false === strpos($key, \'(\')) {\n"
	"            // JSON字段支持\n"
	"            list($field, $name) = explode(\'$.\', $key);\n"
	"            return \'json_extract(\' . $field . \', \\\'$.\' . $name . \'\\\')\';\n"
	"        } elseif (strpos($key, \'.\') && !preg_match(\'/[,\\\'\\\"\\(\\)`\\s]/\', $key)) {\n"
	"            list($table, $key) = explode(\'.\', $key, 2);\n"
	"            if (\'__TABLE__\' == $table) {\n"
	"                $table = $this->query->getTable();\n"
	"            }\n"
	"            if (isset($options[\'alias\'][$table])) {\n"
	"                $table = $options[\'alias\'][$table];\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if ($strict && !preg_match(\'/^[\\w\\.\\*]+$/\', $key)) {\n"
	"            throw new Exception(\'not support data:\' . $key);\n"
	"        }\n"
	"        if (\'*\' != $key && ($strict || !preg_match(\'/[,\\\'\\\"\\*\\(\\)`.\\s]/\', $key))) {\n"
	"            $key = \'`\' . $key . \'`\';\n"
	"        }\n"
	"        if (isset($table)) {\n"
	"            if (strpos($table, \'.\')) {\n"
	"                $table = str_replace(\'.\', \'`.`\', $table);\n"
	"            }\n"
	"            $key = \'`\' . $table . \'`.\' . $key;\n"
	"        }\n"
	"        return $key;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 随机排序\n"
	"     * @access protected\n"
	"     * @return string\n"
	"     */\n"
	"    protected function parseRand()\n"
	"    {\n"
	"        return \'rand()\';\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif